﻿using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Windows.Controls;
using FileTransSystem.ViewModel;

namespace FileTransSystem.Helper
{
    public static class RefreshDataGridHelper
    {
        public static void Refresh(ObservableCollection<SelectableViewModel> selectableViewModels, string cmd,
            object[] param)
        {
            SQLiteConnection sqLiteConnection = GetSqlConnection.Connect();
            DataSet dataSet = DatabaseHelper.ExecuteDataSet(sqLiteConnection, cmd, param);
            DataTable dataTable = dataSet.Tables[0];
            selectableViewModels?.Clear();

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                selectableViewModels.Add(new SelectableViewModel
                {
                    FileName = dataTable.Rows[i]["fileName"].ToString(),
                    FileSize = dataTable.Rows[i]["fileLength"].ToString(),
                    UploadTime = dataTable.Rows[i]["uploadTime"].ToString(),
                    FileMd5 = dataTable.Rows[i]["fileMD5"].ToString(),
                    UploadUser = dataTable.Rows[i]["uploadUser"].ToString(),
                    OwnedBy = dataTable.Rows[i]["ownedBy"].ToString(),
                    FilePath = dataTable.Rows[i]["filePath"].ToString()
                });
            }
        }
    }
}